// 导入表单验证插件
import wxValidate from 'wechat-validate'

Page({
  behaviors: [wxValidate],
  data: {
    houseId: '',
    repairItemId: '',
    mobile: '',
    appointment: '',
    description: '',
    currentDate: Date.now(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
  },
  rules: {
    houseId: [{
      required: true,
      message: '请选择报修房屋!'
    }],
    repairItemId: [{
      required: true,
      message: '请选择维修的项目!'
    }],
    mobile: [{
        required: true,
        message: '请填写手机号码!'
      },
      {
        pattern: /^1[3-8]\d{9}$/,
        message: '请填写正确的手机号码!'
      },
    ],
    appointment: [{
      required: true,
      message: '请选择预约日期!'
    }],
    description: [{
      required: true,
      message: '请填写问题描述!'
    }],
  },
  onLoad({
    id
  }) {
    this.getHouseList()
    this.getRepairItem()

    // 判断有没有id
    if (id) this.getRepairDetail(id)
  },
  // 获取详情的接口
  async getRepairDetail(id) {
    if (!id) return wx.utils.toast('参数有误')
    const {
      code,
      data: repairDetail
    } = await wx.http.get('/repair/' + id)
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      ...repairDetail
    })
  },
  // 获取用户房屋
  async getHouseList() {
    const {
      code,
      data
    } = await wx.http.get('/room')
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    const houseList = data.map(item => {
      return {
        id: item.id,
        name: item.point + item.building + item.room
      }
    })
    this.setData({
      houseList
    })
  },
  // 选择房屋
  selectHouse(e) {
    console.log(e);
    this.setData({
      houseId: e.detail.id,
      houseInfo: e.detail.name
    })
  },
  // 获取维修项目
  async getRepairItem() {
    const {
      code,
      data: repairItem
    } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      repairItem
    })
  },
  selectRepairItem(e) {
    this.setData({
      repairItemId: e.detail.id,
      repairItemName: e.detail.name
    })
  },
  // 获取时间
  getTime(e) {
    // 获取到的时间
    console.log(e.detail);
    this.setData({
      appointment: wx.utils.dataFormat(e.detail),
      dateLayerVisible: false
    })
  },
  // 上传图片
  // 1. 获取到临时图片路径
  // 2. wx.uploadFile 进行上传
  // 3. 将上传成功返回的图片路径放在attachment数组中,就会显示出来图片
  uploadPicture(e) {
    console.log(e);
    // e.detail.file 就是要上传的文件
    wx.uploadFile({
      url: wx.http.baseURL + '/upload', //仅为示例，非真实的接口地址
      filePath: e.detail.file.url,
      name: 'file',
      header: {
        Authorization: 'Bearer ' + getApp().token
      },
      success: (res) => {
        // const data = res.data
        //do something
        console.log(res);
        const data = JSON.parse(res.data)
        console.log(data);
        if (data.code !== 10000) return wx.utils.toast('没上传成功,不要发违法图片')
        // 注意  必须调用setData才能把数据放到data中 让视图发生变化
        const {
          attachment
        } = this.data
        attachment.push(data.data)
        // this.data.attachment.push(data)
        this.setData({
          attachment
        })
      }
    })
  },

  // -------------------------



  openHouseLayer() {
    this.setData({
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },
  openRepairLayer() {
    this.setData({
      repairLayerVisible: true
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },
  // 提交表单
  // 1. 校验表单(校验数据必须在data中有)
  // 2. 通过的话调用接口
  // 3. 跳转页面到列表页
  async goList() {
    // 验证表单数据
    if (!this.validate()) return
    const {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    } = this.data
    const {
      code
    } = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    })
    if (code !== 10000) return wx.utils.toast()
    // 跳转到列表页
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
})